package com.atguigu.distributed.lock.juc;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

class MyThread implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName()+"*******come in Callable");
        try{
            TimeUnit.SECONDS.sleep(2);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 1024;
    }
}

public class CallableDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> futureTask = new FutureTask<>(new MyThread());
        FutureTask<Integer> futureTask2 = new FutureTask<>(new MyThread());
        new Thread(futureTask, "AA").start();
        new Thread(futureTask2, "BB").start();
        int result02 = futureTask.get();
        System.out.println(Thread.currentThread().getName()+"**********");

        int result01 = 100;
        //int result02 = futureTask.get();//要求获得Callble的计算结果，如果没有计算完成就要去强求，会导致阻塞，直到计算完成

        System.out.println("result==>"+(result01+result02));

    }
}
